import request from '@/utils/request'

// 文档生成接口
export interface DocumentGenerateRequest {
  type: string
  subType: string
  content: any
}

export interface DocumentGenerateResponse {
  success: boolean
  data: {
    content: string
    markdown?: string
    html?: string
  }
  message?: string
}

// 生成文档内容
export function generateDocument(data: DocumentGenerateRequest): Promise<DocumentGenerateResponse> {
  return request({
    url: '/api/document/generate',
    method: 'post',
    data
  })
}

// 重新生成文档内容
export function regenerateDocument(data: DocumentGenerateRequest): Promise<DocumentGenerateResponse> {
  return request({
    url: '/api/document/regenerate',
    method: 'post',
    data
  })
}

// 导出文档
export function exportDocument(data: {
  type: string
  subType: string
  content: string
  format: 'html' | 'markdown' | 'word'
}): Promise<Blob> {
  return request({
    url: '/api/document/export',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

// AI梳理会议内容接口
export interface MeetingProcessRequest {
  meeting_str: string
}

export interface AgendaItem {
  agenda_title: string
  agenda_context: string[]
}

export interface MeetingProcessResponse {
  success: boolean
  agendas: AgendaItem[]
  message?: string
}

// AI梳理会议内容
export function processMeetingContent(meetingStr: string): Promise<MeetingProcessResponse> {
  return request({
    url: `/api/ai_chat/create_meeting`,
    method: 'post',
    data: {
      meeting_str: meetingStr
    }
  })
}

// 会议纪要提交接口
export interface MeetingMinutesRequest {
  doc_id: string
  issue_office: string
  issue_year: number
  issue_month: number
  issue_day: number
  meeting_name: string
  meeting_date: string
  meeting_place: string
  meeting_leader: string
  meeting_overview: string
  attendees_list: string
  primary_recipients: string
  cc_recipients: string
  agendas: any[]
}

export interface MeetingMinutesResponse {
  success: boolean
  data?: string[] // 图片的base64数据数组
  message?: string
}

// 提交会议纪要
export function submitMeeting(data: MeetingMinutesRequest): Promise<string[]> {
  return request({
    url: '/api/document_assistant/meeting/minutes/preview/all',
    method: 'post',
    data
  })
}

// 导出会议纪要
export function downloadMeetingMinutes(data: MeetingMinutesRequest): Promise<Blob> {
  return request({
    url: '/api/document_assistant/meeting/minutes',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

// 复函生成接口
export interface ReplyLetterRequest {
  meeting_str: string
}

export interface ReplyLetterResponse {
  success: boolean
  data: string
  message?: string
}

// 生成复函内容
export function generateReplyLetter(data: ReplyLetterRequest): Promise<ReplyLetterResponse> {
  return request({
    url: '/api/ai_chat/letter_production',
    method: 'post',
    data
  })
}

// 模拟文档生成（用于开发阶段）
export function mockGenerateDocument(data: DocumentGenerateRequest): Promise<DocumentGenerateResponse> {
  return new Promise((resolve) => {
    setTimeout(() => {
      const mockResponse: DocumentGenerateResponse = {
        success: true,
        data: {
          content: `这是模拟生成的${data.subType}内容`,
          markdown: `# ${data.subType}\n\n这是模拟生成的Markdown内容`,
          html: `<h1>${data.subType}</h1><p>这是模拟生成的HTML内容</p>`
        }
      }
      resolve(mockResponse)
    }, 2000)
  })
}
